<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <title>Carver-Markdown</title>
    <link rel="stylesheet" href="./editor.md/css/editormd.min.css"/>
    <link rel="stylesheet" href="./bootstrap-5.1.3-dist/css/bootstrap.css" />
    <style type="text/css" media="all">
    
    .CodeMirror{
        z-index: 0 !important;
        width: 630.5px !important;
    }
        
    </style>
</head>

<body>
   
    
<!--Markdown编译器-->
<div id="carver-editor">

    <textarea style="display:none;" id="carver_content"># 请写出你的想法 !</textarea>
</div>


<div class="container">
    
  <div class="row">
      
    <button type="button" class="btn btn-outline-success col-md-6" id="carver-btn" style="margin:auto auto">点击发布</button>
    <button type="button" class="btn btn-outline-success col-md-6" id="carver-save-btn" style="margin:auto auto">点击保存</button>
  </div>
  
  
  
<div id="myModal" class="modal fade" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认发布</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p class="text-center">真的需要立即发布您的作品吗？😄</p>
                <p class="text-secondary text-center"><small>发布之后不能立即撤销～</small></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="submit_send">确认发布</button>
            </div>
        </div>
    </div>
</div>
 
</div>


</body>
</html>
<script src="./jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="./editor.md/editormd.min.js"></script>
<script type="text/javascript" src="./bootstrap-5.1.3-dist/js/bootstrap.js"></script>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>  

<script type="text/javascript">

    $(function () {
        
        //是否支持本地缓存
        if (window.localStorage) {
            console.log('This browser supports localStorage');
        } else {
            alert('This browser does NOT support');
        }
        
        
        //获取历史数据
        var last_content = "请写出你的想法!";
        var last_time = window.localStorage.getItem("get_index_time")??getStartEndTime(1);
        
        
        $.ajax({
    			url:'update.php',
    			async: false,  
    			data:{action:'getCache',key:'carver-save',time:last_time},
    			type:"GET",
    			dataType:'json',
    			success:function(data){
    			    if(data.code==200){
                        window.localStorage.setItem("get_index_time",(new Date).getTime() / 1000);
                        last_content = data.data.resource;
    			    }else{
    			        var msg = data.msg;
                        var system_msg = 'error';
                        ShowTip(msg,system_msg);
    			    }
    			    
    			    
    			}
		    })
		    

        var editor = editormd("carver-editor", {
            width: "100%",
            height: "600px",
            markdown: last_content,
            path: "./editor.md/lib/"  // Autoload modules mode, codemirror, marked... dependents libs path
        });
        
        
        $("#carver-btn").click(function (){
            $("#myModal").modal("show");
        })
        
        $("#carver-save-btn").click(()=>{
            //获取当前保存的内容和保存时间
            var time = window.localStorage.getItem("time");
            var save_content=$("#carver_content").val();
            
            $.ajax({
    			url:'update.php',
    			data:{content:save_content,action:'setCache',key:'carver-save',time:time},
    			type:"GET",
    			dataType:'json',
    			success:function(data){
    			    if(data.code==200){
    			        var msg = data.msg;
    			        var system_msg = 'success';
                        window.localStorage.setItem("time",(new Date).getTime() / 1000);
    			    }else{
    			        var msg = data.msg;
                        var system_msg = 'error';
    			    }
    			    ShowTip(msg,system_msg);
    			    
    			}
		    })
            
            
        })
        
        $("#submit_send").click(function(){
            
            $("#myModal").modal("hide");
            var content=$("#carver_content").val();
            var time = window.localStorage.getItem("time")
            
            $.ajax({
    			url:'update.php/save',
    			data:{content:content,action:'save',time:time},
    			type:"GET",
    			dataType:'json',
    			success:function(data){
    			    if(data.code==200){
    			        var msg = data.msg;
                        var system_msg = 'success';
                        console.log()
                        window.localStorage.setItem("time",(new Date).getTime() / 1000)
    			        
    			    }else{
    			        var msg = data.msg;
                        var system_msg = 'error';
    			    }
    			    ShowTip(msg,system_msg);
    			    
    			}
		    })
        
        })
        

    });
    
    
    
    //调用方法
    function ShowTip(tip, type) {
  
    var $tip = $('#tip');
    if ($tip.length == 0) {
      if(type == 'success') {
        $tip = $('<div id="tip" style="min-width: 160px;-webkit-box-sizing: border-box; box-sizing: border-box;  border-radius: 10px;  border-width: 1px; border-style: solid;border-color: #EBEEF5;position: fixed;left: 50%;top: 100px; height: 73px; line-height: 48px;text-align: center;background: #D1E7DD;"></div>');
        $('#carver-editor').append($tip);
      } else {
          $tip = $('<div id="tip" style="min-width: 160px;-webkit-box-sizing: border-box; box-sizing: border-box;  border-radius: 10px;  border-width: 1px;color:#A02B39; border-style: solid;border-color: #EBEEF5;position: fixed;left: 50%;top: 100px; height: 73px; line-height: 48px;text-align: center;background: #F8D7DB;"></div>');
        $('#carver-editor').append($tip);
      } 
    }
    $tip.stop(true).attr('class', 'alert alert-' + type).text(tip).css('margin-left', -$tip.outerWidth() / 2).fadeIn(500).delay(2000).fadeOut(500);
}

//获取某天的起止时间戳（今日传入0，昨日传入1，明日传入-1）
        function getStartEndTime (num = 0) {
            // 一天的毫秒数
            const MillisecondsADay = 24*60*60*1000 * num
            // 今日开始时间戳
            const todayStartTime = new Date(new Date().setHours(0, 0, 0, 0)).getTime()
            // 今日结束时间戳
            const todayEndTime = new Date(new Date().setHours(23,59,59,999)).getTime()
            // 昨日开始时间戳
            const yesterdayStartTime = todayStartTime - MillisecondsADay
            // 昨日结束时间戳
            const yesterdayEndTime = todayEndTime - MillisecondsADay
            return [
                parseInt(yesterdayStartTime / 1000),
                parseInt(yesterdayEndTime / 1000)
            ]
        }



    
    

  
 
</script>

